﻿#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "Public/appsignal.h"
#include "Control/Message/messagewidget.h"

#include <QFileDialog>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    init();

    setWindowTitle("Linte ");
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::init()
{
    connect(ui->btnOpenFile, &QPushButton::clicked, this, &MainWindow::slot_btn_open_file_click);
    connect(ui->btnClosePoint, &QPushButton::clicked, this, &MainWindow::slot_btn_close_point_click);
    connect(ui->btnResetCamera, &QPushButton::clicked, this, &MainWindow::slot_btn_reset_camera_click);
    connect(ui->btnDelete, &QPushButton::clicked, this, &MainWindow::slot_btn_delete_point_click);
    connect(ui->btnDeleteCancel, &QPushButton::clicked, this, &MainWindow::slot_btn_delete_cancel_click);
    connect(ui->btnExport, &QPushButton::clicked, this, &MainWindow::slot_btn_export_point_click);
    connect(ui->btnShowLine, &QPushButton::clicked, this, &MainWindow::slot_btn_show_line_click);
    connect(ui->btnShowGrid, &QPushButton::clicked, this, &MainWindow::slot_btn_show_grid_click);
    connect(ui->btnShowTiff, &QPushButton::clicked, this, &MainWindow::slot_btn_show_tiff_click);
    connect(ui->btnModeNormal, &QPushButton::clicked, this, &MainWindow::slot_btn_mode_default_click);
    connect(ui->btnModeLongitude, &QPushButton::clicked, this, &MainWindow::slot_btn_mode_longitude_click);
    connect(ui->btnModeLatitude, &QPushButton::clicked, this, &MainWindow::slot_btn_mode_latitude_click);

    connect(AppSignal::getInstance(), &AppSignal::sgl_occur_system_error, this, &MainWindow::slot_occur_system_error);
    connect(AppSignal::getInstance(), &AppSignal::sgl_occur_system_info, this, &MainWindow::slot_occur_system_info);
    connect(AppSignal::getInstance(), &AppSignal::sgl_occur_system_success, this, &MainWindow::slot_occur_system_success);

    //mCustomFileParser.parse("C:/Users/admin/Desktop/L04_20220510155521.txt");

    ui->lbVersion->setText(QString("V %1").arg(QApplication::applicationVersion()));
    showMaximized();
}

void MainWindow::slot_occur_system_error(const QString &msg)
{
    MessageWidget *widget = new MessageWidget(MessageWidget::M_Error, MessageWidget::P_Top_Center, this);
    widget->showMessage(msg);
}

void MainWindow::slot_occur_system_info(const QString &msg)
{
    MessageWidget *widget = new MessageWidget(MessageWidget::M_Info, MessageWidget::P_Top_Center, this);
    widget->showMessage(msg);
}

void MainWindow::slot_occur_system_success(const QString &msg)
{
    MessageWidget *widget = new MessageWidget(MessageWidget::M_Success, MessageWidget::P_Top_Center, this);
    widget->showMessage(msg);
}

void MainWindow::slot_btn_open_file_click()
{
    QString filter;
    QStringList fileNameList = QFileDialog::getOpenFileNames(this, "打开文件", "", "支持的文件 (*.txt *.csv *.xlsx *.xls *.tif);;文本文件 (*.txt);;文本文件 (*.csv);;表格文件 (*.xls);;表格文件 (*.xlsx)", &filter);
    if (fileNameList.isEmpty()) return;
    for (auto &path : fileNameList)
    {
        mCustomFileParser.parse(path);
    }
}

void MainWindow::slot_btn_close_point_click()
{
    ui->widgetPointView->clear();
}

void MainWindow::slot_btn_reset_camera_click()
{
    ui->widgetPointView->focusProp();
}

void MainWindow::slot_btn_delete_point_click()
{
    ui->widgetPointView->deleteSelectPoint();
}

void MainWindow::slot_btn_delete_cancel_click()
{
    ui->widgetPointView->restorePreviousDelete();
}

void MainWindow::slot_btn_export_point_click()
{
    ui->widgetPointView->exportPoint();
}

void MainWindow::slot_btn_show_line_click()
{
    ui->widgetPointView->showLine(ui->btnShowLine->isChecked());
}

void MainWindow::slot_btn_show_grid_click()
{
    ui->widgetPointView->showGrid(ui->btnShowGrid->isChecked());
}

void MainWindow::slot_btn_show_tiff_click()
{
    ui->widgetPointView->showTiff(ui->btnShowTiff->isChecked());
}

void MainWindow::slot_btn_mode_default_click()
{
    ui->widgetPointView->setPointMode(0);
}

void MainWindow::slot_btn_mode_longitude_click()
{
    ui->widgetPointView->setPointMode(1);
}

void MainWindow::slot_btn_mode_latitude_click()
{
    ui->widgetPointView->setPointMode(2);
}

